import easywebdav
# py3ports.py
import easywebdav.client
easywebdav.basestring = str
easywebdav.client.basestring = str

class WebDAVClient:
    def __init__(self, hostname, username, password, protocol='http', port=443, path="/dav"):
        self.client = easywebdav.connect(
            hostname,
            username=username,
            password=password,
            protocol=protocol,
            port=port,
            path=path
        )

    def upload_file(self, local_path, remote_path):
        self.client.upload(local_path, remote_path)
        print(f'Uploaded {local_path} to {remote_path}')

    def download_file(self, remote_path, local_path):
        self.client.download(remote_path, local_path)
        print(f'Downloaded {remote_path} to {local_path}')

    def delete_file(self, remote_path):
        self.client.delete(remote_path)
        print(f'Deleted {remote_path}')

    def create_directory(self, remote_path):
        self.client.mkdir(remote_path)
        print(f'Created directory {remote_path}')

    def delete_directory(self, remote_path):
        self.client.rmdir(remote_path)
        print(f'Deleted directory {remote_path}')

    def list_directory(self, remote_path):
        files = self.client.ls(remote_path)
        print(f'List of files in {remote_path}:')
        for f in files:
            print(f.name)
        return files


def get_default_client():
    hostname = "81.70.84.40"
    username = "caoyiming233@126.com"
    password = "<need default password>"
    client = WebDAVClient(
        hostname, 
        username, 
        password,
        protocol="http",
        port=5212,
        path="/dav"
    )
    return client

def GetDefaultDavClient():
    return get_default_client()


# 示例使用
if __name__ == "__main__":
    hostname = "81.70.84.40"
    username = "caoyiming233@126.com"
    password = ""
    
    client = WebDAVClient(
        hostname, 
        username, 
        password,
        protocol="http",
        port=5212,
        path="/dav"
    )
    
    # # 上传文件
    # client.upload_file('local_file.txt', '/kghallu/remote_file.txt')
    
    # # 下载文件
    # client.download_file('/remote_path/remote_file.txt', 'local_file.txt')
    
    # # 删除文件
    # client.delete_file('/remote_path/remote_file.txt')
    
    # # 创建目录
    # client.create_directory("/kghallu")
    
    # # 删除目录
    # client.delete_directory('/remote_path/new_directory')
    
    # 列出目录内容
    # client.list_directory('paper')
    # client.list_directory('/')

